package com.wlhse.util;

import com.wlhse.dao.ProblemFactorDao;
import com.wlhse.entity.ProblemFactorPojo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;

@Component
public class PoiProblemUtil {

    @Resource
    private ProblemFactorPojo problemFactorPojo;

    @Resource
    private SortCodeUtil sortCodeUtil;

    @Resource
    private ProblemFactorDao problemFactorDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Transactional
    public void excelToDB(Workbook wb) {
        try {
            Sheet sheet;
            Row row;
            for (int sheetnum = 0; sheetnum < wb.getNumberOfSheets(); sheetnum++) {
                sheet = wb.getSheetAt(sheetnum);
                String level1Code = "";
                String level2Code;

                for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
                    row = sheet.getRow(j);
                    int columnNum = row.getPhysicalNumberOfCells();
                    Iterator<Cell> cell = row.iterator();
                    int flag = 0;
                    while (cell.hasNext()) {
                        if (flag == columnNum) {
                            break;
                        } else {
                            String myString = cell.next().toString();
                            while (myString.equals("")) {
                                myString = cell.next().toString();
                                flag++;
                            }
                            switch (flag) {
                                case 0:
                                    level1Code = problemFactorDao.queryNameByLenthAndCode(4, "", myString);

                                    if (level1Code == null) {
                                        String tempCode = problemFactorDao.getMaxString("", 0, 4);
                                        if (tempCode == null) {
                                            level1Code = "0001";
                                        } else {
                                            level1Code = sortCodeUtil.getMaxCodeString(tempCode);
                                        }
                                        problemFactorPojo.setProblemFactorCode(level1Code);
                                        problemFactorPojo.setProblemFactorName(myString);
                                        String hash = HashUtil.hash(problemFactorPojo.getProblemFactorCode() + problemFactorPojo.getProblemFactorName());
                                        problemFactorPojo.setProblemFactorHash(hash);
                                        problemFactorDao.insertSingle(problemFactorPojo);
                                    }
                                    break;
                                case 1:
                                    level2Code = problemFactorDao.queryNameByLenthAndCode(6, level1Code, myString);
                                    if (level2Code == null) {
                                        String tempCode = problemFactorDao.getMaxString(level1Code, 4, 6);
                                        if (tempCode == null) {
                                            level2Code = level1Code + "01";
                                        } else {
                                            level2Code = sortCodeUtil.getMaxCodeString(tempCode);
                                        }
                                        problemFactorPojo.setProblemFactorCode(level2Code);
                                        problemFactorPojo.setProblemFactorName(myString);
                                        String hash = HashUtil.hash(problemFactorPojo.getProblemFactorCode() + problemFactorPojo.getProblemFactorName());
                                        problemFactorPojo.setProblemFactorHash(hash);
                                        problemFactorDao.insertSingle(problemFactorPojo);
                                    }
                                    break;
                            }
                            flag++;
                        }
                    }
                }
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}